#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
用户交互模块

包含所有与用户交互相关的函数
"""

import os
from utils import get_input, print_colored, validate_package_name, validate_project_name

def get_project_type():
    """
    获取项目类型
    
    Returns:
        str: 'app' 或 'module'
    """
    print_colored("\n请选择项目类型:", "blue")
    print_colored("1. 应用 (app)", "cyan")
    print_colored("2. 模块 (module)", "cyan")
    
    while True:
        project_type = get_input("\n请输入选项 [1-2]", "1")
        if project_type in ['1', '2']:
            return 'app' if project_type == '1' else 'module'
        print_colored("错误: 请输入1或2选择项目类型", "red")

def get_template_type():
    """
    获取模板类型
    
    Returns:
        str: '1' 或 '2'
    """
    print_colored("\n请选择模板类型:", "blue")
    print_colored("1. 基础版 (基本功能)", "cyan")
    print_colored("2. 增强版 (包含额外功能)", "cyan")
    
    while True:
        template_type = get_input("\n请输入选项 [1-2]", "1")
        if template_type in ['1', '2']:
            return template_type
        print_colored("错误: 请输入1或2选择模板类型", "red")

def get_project_info(source_dir, project_type):
    """
    获取项目信息
    
    Args:
        source_dir (str): 源目录路径
        project_type (str): 项目类型 ('app'或'module')
        
    Returns:
        tuple: (project_name, package_name, display_name, parent_dir, target_dir)
    """
    while True:
        default_name = "hzy_example_module" if project_type == 'module' else "hzy_example_project"
        project_name = get_input("请输入新项目名称（小写字母、数字和下划线）", default_name)
        if validate_project_name(project_name):
            break
    
    while True:
        package_name = get_input("请输入包名", f"com.example.{project_name}")
        if validate_package_name(package_name):
            break
    
    display_name = get_input("请输入项目显示名称", f"{project_name.replace('_', ' ').title()}")
    
    # 导入桌面路径获取模块
    from desktop_path import get_desktop_path
    # 默认使用用户桌面路径而不是脚本所在目录
    parent_dir = get_input("请输入项目存放路径", get_desktop_path())
    target_dir = os.path.join(parent_dir, project_name)
    
    return project_name, package_name, display_name, parent_dir, target_dir

def confirm_overwrite(target_dir):
    """
    确认是否覆盖已存在的目录
    
    Args:
        target_dir (str): 目标目录路径
        
    Returns:
        bool: 是否覆盖
    """
    if os.path.exists(target_dir):
        print_colored(f"\n警告: 目标目录 {target_dir} 已存在!", "yellow")
        overwrite = get_input("是否覆盖？(y/n)", "n").lower()
        if overwrite == 'y':
            print_colored("将覆盖目标目录", "red")
        else:
            print_colored("操作已取消", "green")
        return overwrite == 'y'
    return True


def get_ide_choice():
    """
    获取用户选择的IDE类型
    """
    print_colored("\n请选择用于打开项目的开发工具:", "blue")
    print_colored("1. Visual Studio Code", "cyan")
    print_colored("2. Android Studio", "cyan")
    print_colored("3. 直接打开项目文件夹", "cyan")
    
    choice = get_input("请选择 [1-3]", "1")
    
    ide_map = {
        "1": "vscode",
        "2": "android_studio",
        "3": "open_folder"
    }
    
    return ide_map.get(choice, "vscode")